package com.ouyang.task;

import com.alibaba.dashscope.aigc.generation.Generation;
import com.alibaba.dashscope.aigc.generation.GenerationParam;
import com.alibaba.dashscope.aigc.generation.GenerationResult;
import com.alibaba.dashscope.common.Message;
import com.alibaba.dashscope.common.Role;
import com.alibaba.dashscope.utils.Constants;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.ouyang.mapper.po.QuestionPO;
import com.ouyang.service.QuestionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.Arrays;
import java.util.List;

@Component
public class QuestionTask {

    public final static String API_KEY="sk-ab881de9f93e487c9d7af61f451e45b2";

    @Autowired
    private QuestionService questionService;
   // @Scheduled(fixedDelay = 20*1000)
    public void reportCurrentTimeWithFixedDelay() throws Exception{
        System.out.println("开始执行定时任务------------------------------》");
        String result=aLiQianWenLLM();
        List<QuestionPO> questionPOList= JSON.parseArray(result, QuestionPO.class);
        questionService.addQuestionFromAI(questionPOList);
    }

    public  String  aLiQianWenLLM() throws Exception {
        Constants.apiKey=API_KEY;
        Generation gen = new Generation();

        Message systemMsg = Message.builder()
                .role(Role.SYSTEM.getValue())
                .content("You are a helpful assistant.")
                .build();
        Message userMsg = Message.builder()
                .role(Role.USER.getValue())
                .content("我现在想做一个脑力问答游戏，请给我10道题，随机，涉及世界历史方面，世界科学方面，世界生物方面，世界物理方面，世界地理方面 ，每个出2道题，1道容易，1道困难，给出题目，答案，以及4个选项 给我返回如下的json数组格式数据类似下面[{\n" +
                        "      \"question\":\"地球的半径是多长\",\n" +
                        "      \"answer\":\"A\",\n" +
                        "      \"level\":\"简单\",\n" +
                        "      \"category\":\"世界历史\",\n" +
                        "      \"A\":\"6731KM\",\n" +
                        "      \"B\":\"6371KM\",\n" +
                        "      \"C\":\"6271KM\",\n" +
                        "      \"D\":\"6771KM\"\n" +
                        "  }]")
                .build();

        GenerationParam param = GenerationParam.builder()
                .model("qwen-turbo")
                .messages(Arrays.asList(systemMsg, userMsg))
                .resultFormat(GenerationParam.ResultFormat.MESSAGE)
                .topP(0.8)
                .build();
        long startTime=System.currentTimeMillis();
        System.out.println("开始调用时间---->"+startTime);
        GenerationResult generationResult=gen.call(param);
        System.out.println(JSONObject.toJSONString(generationResult));
        String context=generationResult.getOutput().getChoices().get(0).getMessage().getContent();
        String[] arr= context.split("\\[");
        String[] arr2=arr[1].split("\\]");
        System.out.println(arr2[0]);
        String result="["+arr2[0]+"]";
        long endTime=System.currentTimeMillis();
        System.out.println("结束调用时间---->"+startTime);
        System.out.println("调用时长ms---->"+(endTime-startTime));
        return result;
    }
}
